from django.db import models
from django.forms import ModelForm
from trees.constants import *
import datetime, time


# class SpeciesManager(models.Manager):
#     '''Custom manager to let us query for most popular species'''
#     
#     def top_species(self, max=15):
#         top = self.model.objects.values('name').annotate(score=Count('entry')).order_by('-score')[:max]
#         return [tag['name'] for tag in top]
        

class Species(models.Model):
    '''Plant species'''

    latin_name = models.CharField(max_length=255, blank=True)
    common_name = models.CharField(max_length=255, blank=True)
    lastcount = models.IntegerField(blank=True, null=True) # Caches count of plants in this species

    class Meta:
        verbose_name_plural = "Species"
        
    def __unicode__(self):
        return "%s (%s)" % (self.latin_name, self.common_name)





class Plant(models.Model):
    '''Individual plant object (usually trees)'''

    legal_status = models.IntegerField(max_length=3, choices=LEGAL_STATUS_CHOICES, blank=True)
    specie = models.ForeignKey(Species,null=True,blank=True)
    species_o = models.CharField(max_length=384, blank=True)
    address = models.CharField(max_length=384, blank=True)
    address_order = models.IntegerField(null=True, blank=True)
    site_type = models.CharField(max_length=6, choices=SITE_TYPE_CHOICES, blank=True)    
    plant_type = models.IntegerField(max_length=2, choices=PLANT_TYPE_CHOICES, blank=True)    
    caretaker = models.IntegerField(max_length=2, choices=CARETAKER_CHOICES, blank=True)        
    planting_date = models.DateField(null=True,blank=True)
    dbh = models.IntegerField(null=True, blank=True)
    plot_size_o = models.CharField(max_length=384, blank=True)
    permit_notes = models.CharField(max_length=765, blank=True)
    xcoord_o = models.CharField(max_length=384, blank=True)
    ycoord_o = models.CharField(max_length=384, blank=True)

    # class Meta:
    #     db_table = u'treedata'
        
    def __unicode__(self):
        return "%s (%s)" % (self.address, self.address_order)


class Inquiry(models.Model):
    '''New tree request'''

    name = models.CharField(max_length=100)
    email = models.EmailField(blank=True)
    species = models.ForeignKey(Species, blank=True,null=True,help_text="Please select species.")
    comments = models.TextField(blank=True)



class Rx(models.Model):
    '''Store maintenance requests for a particular plant'''

    plant = models.ForeignKey(Plant)
    submittor = models.CharField(max_length=255)
    email = models.EmailField()
    submit_date = models.DateField(default=datetime.datetime.today)
    completed = models.BooleanField(default=False)
    completed_date = models.DateField(null=True,blank=True)    
    comments = models.TextField()


    class Meta:
        verbose_name_plural = "Maintenance Requests"
     
        
    def __unicode__(self):
        return "%s (%s)" % (self.plant, self.submittor)

